package com.ebt.m.commons.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;


import com.ebt.m.commons.BaseApplication;

import java.util.List;

/**
 * Created by wanglu on 2017/5/2.
 */

public class SystemUtil {

    /**
     * 判断应用是否已经启动
     *
     * @param packageName 要判断应用的包名
     * @return boolean
     */
    public static boolean isAppAlive(String packageName) {
        ActivityManager activityManager =
                (ActivityManager) BaseApplication.getBaseApplication().getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> processInfos
                = activityManager.getRunningAppProcesses();
        for (int i = 0; i < processInfos.size(); i++) {
            if (processInfos.get(i).processName.equals(packageName)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isBackground(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(context.getPackageName())) {
                if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
                        || appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
                    return false;
                } else {
                    return true;
                }
            }
        }
        return false;
    }

}
